package com.wufun.overseas.main.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.wufun.overseas.R;
import com.wufun.overseas.main.base.MyActivity;
import com.wufun.overseas.main.base.MyApplication;

import java.io.File;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author: hest
 * Date:  2016/4/13
 * 面对面传游戏入口页面
 */
public class FaceTransferHomePageActivity extends MyActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.face_transfer_homepage_activity);
        ButterKnife.bind(this);
    }

    public static Intent buildIntent(Context context, String title) {
        Intent intent = new Intent(context, FaceTransferHomePageActivity.class);
        intent.putExtra("title", title);
        return intent;
    }

    @Override
    public void initWidget(View... v) {
        super.initWidget(v);
        initView();
    }

    void initView() {
        setBarTitleTx(getIntent().getStringExtra("title"));
        initNoPapaTipView();
    }

    private void initNoPapaTipView() {
        String linkText = getResources().getString(R.string.linktext_str);
        String tip = getString(R.string.no_papa_tip, linkText);

        SpannableString spStr = new SpannableString(tip);
        ClickableSpan clickSpan = new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#3CA4FD")); //设置链接的文本颜色
                ds.clearShadowLayer();

//                ds.setColor(ds.linkColor); //设置链接的文本颜色
//                ds.setUnderlineText(false); //去掉下划线
            }

            @Override
            public void onClick(View widget) {
                Context ctx = widget.getContext();
                avoidHintColor(widget);

                sharePapa(ctx);
            }

            private void avoidHintColor(View view) {
                if (view instanceof TextView)
                    ((TextView) view).setHighlightColor(getResources().getColor(android.R.color.transparent));
            }

            private void sharePapa(Context ctx) {

                File file = null;
                try {
                    file = new File(getPackageManager().getApplicationInfo(ctx.getPackageName(), 0).sourceDir);

                    //多种文件
                    Intent share = new Intent(Intent.ACTION_SEND);
                    String mimeType = "*/*";
                    mimeType = "application/zip";
                    share.setType(mimeType);
                    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                    startActivity(Intent.createChooser(share, "发送悟饭（蓝牙发送免流量）"));
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }; //设置超链接
        int start = tip.indexOf(linkText);
        int end = start + linkText.length();
        spStr.setSpan(clickSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//        nopapaTip.setText(spStr);
//        nopapaTip.setMovementMethod(LinkMovementMethod.getInstance());//设置超链接为可点击状态
    }

    @OnClick({R.id.face_transfer_rel_create_hotspot, R.id.face_transfer_rel_join_hotspot, R.id.nopapaTip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.face_transfer_rel_create_hotspot://创建热点
                startActivity(new Intent(mContext, BuildQRCodeActivity.class));
                break;
            case R.id.face_transfer_rel_join_hotspot://加入热点
                startActivity(new Intent(this, FaceTransferScanQrCodeActivity.class));
                break;
            case R.id.nopapaTip:
//                IntentUtil.getInstance().goFormDetial(this, "3269874");
                break;
        }
    }
}
